using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.RepositoryUIs
{
	[ToolboxItem(false)]
	public partial class UCRepositoryItemListEx : UserControl
	{
		#region Initialization
		public UCRepositoryItemListEx(IServiceProvider serviceProvider)
		{
			_serviceProvider = serviceProvider;

			InitializeComponent();

			init();
		}
		private void init()
		{
			_bShowFoldersInTabs = true;
			_repositoryItem = null;

			// 
			// ucList
			// 
			this.ucList = new OpenComposite.EII.RepositoryUIs.UCRepositoryItemList(_serviceProvider);
			this.ucList.FolderID = -1;
			this.ucList.Location = new System.Drawing.Point(29, 193);
			this.ucList.Name = "ucList";
			this.ucList.Size = new System.Drawing.Size(238, 126);
			this.ucList.TabIndex = 2;
			this.ucList.Visible = false;
			this.Controls.Add(this.ucList);

			tabCtrl.Dock = DockStyle.Fill;
			lblInfo.Dock = DockStyle.Fill;
			ucList.Dock = DockStyle.Fill;
		}
		#endregion

		#region Public Members

		#region Properties

		public bool ShowFoldersInTabs
		{
			get { return _bShowFoldersInTabs; }
			set
			{
				if ( _bShowFoldersInTabs != value ) {
					_bShowFoldersInTabs = value;
					OnShowFoldersInTabsChanged();
				}
			}
		}
		public RepositoryItemBase RepositoryItem
		{
			get { return _repositoryItem; }
			set
			{
				if ( _repositoryItem != value ) {
					_repositoryItem = value;
					OnRepositoryItemChanged();
				}
			}
		}

		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected virtual void OnShowFoldersInTabsChanged()
		{
			updateView();
		}
		protected virtual void OnRepositoryItemChanged()
		{
			updateView();
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables

		private bool _bShowFoldersInTabs;
		private RepositoryItemBase _repositoryItem;
		private IServiceProvider _serviceProvider;

		#endregion

		#region Methods

		private void updateView()
		{
			if ( this.RepositoryItem == null ) {
				lblInfo.Visible = true;
				lblInfo.Text = "No Repository Item Defined.";
				tabCtrl.Visible = false;
				return;
			}
			if ( this.ShowFoldersInTabs ) {
				ucList.Visible = false;
				createAndFillTabbedLists();
				if ( tabCtrl.TabCount == 0 ) {
					tabCtrl.Visible = false;
					lblInfo.Text = "No Items Available.";
					lblInfo.Visible = true;
				} else {
					tabCtrl.Visible = true;
					lblInfo.Visible = false;
				}
			} else {
				ucList.SetParentRepositoryItem(this.RepositoryItem.RepositoryItemType, this.RepositoryItem.ID);
				ucList.Visible = true;
				tabCtrl.Visible = false;
				lblInfo.Visible = false;
			}
		}

		private void createAndFillTabbedLists()
		{
			tabCtrl.TabPages.Clear();
			DataTable dtFolders = Global.Data.GetRepositoryHierarchyChildren(_repositoryItem);
			if ( dtFolders == null || dtFolders.Rows.Count == 0 ) {
				return;
			}
			foreach ( DataRow row in dtFolders.Rows ) {
				if ( (int)row["child_count"] > 0 && (ItemType)row["child_reptype_id"] == ItemType.GenericFolder ) {
					TabPage tp = new TabPage();
					tp.Text = row["child_display"].ToString();
					UCRepositoryItemList objlist = new UCRepositoryItemList(_serviceProvider);
					objlist.Dock = DockStyle.Fill;
					objlist.FolderID = (int)row["child_id"];
					tp.Controls.Add(objlist);
					tabCtrl.TabPages.Add(tp);
				}
			}
		}
		#endregion

		#region Event Handlers
		#endregion

		#endregion
	}
}
